<template>
  <div class="container">
    <div class="main">
      <topBar />
      <div class="main_content">
        <div style="height: 50px;"></div>
        <div class="content" v-for="i in postsData" @click="getCom(i.postID)">
          <div class="content_item">
            <div class="content_item_left">
                <el-avatar size="large" :src="i.posterimg" class="avatar_img"/>
            </div>
            <div class="content_item_right">
              <div class="item_info">
                <div class="p_top">
                  <div class="p_username">
                    <p>{{i.postername}}</p>
                  </div>
                  <div class="p_postTime">
                    <p>{{i.createTime}}</p>
                  </div>
                </div>
                <div class="p_bottom">
                  <p style="font-size: 16px">{{ i.title }}</p>
                  <p style="color: #777777;font-size: 14px;">{{ i.content }}</p>
                </div>
              </div>
              <div class="p_img">
                <img :src="i.img">
              </div>
              <div class="item_control">
                <div class="comment item_control_item" style="display: flex;">
                  <i class="iconfont nt-pinglun1"></i>
                  <p>评论</p>
                </div>
                <div class="star item_control_item" style="display: flex;">
                  <i class="iconfont nt-xihuan" @click.stop="star(i.postID)"></i>
                  <p>{{i.stars}}</p>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { ref , onMounted } from 'vue';
import { useRoute } from 'vue-router'
import { getAllPost , starPost } from '@/api/posts'
import { getComment } from '@/api/comment'
// 引入pinia中的useStore库
import { useStore } from '@/stores/userData'
// 引入storeToRefs，使普通数据变为响应式数据
import { storeToRefs } from "pinia";
// 接收useStore
const store = useStore()
// 解构并使数据具有响应式
const { userData , commentData , current_post_id } = storeToRefs(store);
const route = useRoute()
onMounted(() => {
  if(route.query.cls){
    cls.value = route.query.cls;
  }
  if(route.query.posterId){
    posterId.value = route.query.posterId;
  }
  getAllPostData()
})
const cls = ref('')
const posterId = ref('')
const postsData = ref({})
const searchContent = ref('')
const avatarUrl = ref('')
const getAllPostData = () => {
  let cl = cls.value
  let pid = posterId.value
  let content = searchContent.value
  getAllPost(1,cl,pid,content).then((res) => {
    console.log(res.data.records);
    postsData.value = res.data.records
  })
}
const getCom = (postID) => {
  console.log(postID);
  current_post_id.value = postID
  getComment(postID).then((res) => {
    commentData.value = res.data.records
  })
}
const star = (postID) => {
  starPost(postID).then((res) => {
    console.log(res);
    if(res.code == 1){
      ElNotification({
        title: '点赞成功',
        type: 'success',
      })
    }
    let cl = cls.value
    let pid = posterId.value
    let content = searchContent.value
    getAllPost(1,cl,pid,content).then((res) => {
      console.log(res.data.records);
      postsData.value = res.data.records
    })
  })
}
</script>
<style scoped lang="scss">
.container{
  height: 100vh;
  position: relative;
}
.main{
  height: 90vh;
  width: 100%;
  position: absolute;
  background-color: #fff;
  top: calc(50% - 45vh);
  transition: 0.5s;
  overflow: hidden;
}
.main_content{
  height: 90vh;
  width: 100%;
  overflow-y: scroll;
}
.main_content::-webkit-scrollbar{
  width: 5px;
  border-radius: 30px;
}
.main_content::-webkit-scrollbar-thumb{
  width: 5px;
  border-radius: 30px;
  background-color: #BFBFBF;
}
.content{
  display: grid;
  grid-template-rows: 1fr auto;
  cursor: pointer;
}
.content_item_right_hr{
  margin-top: -10px;
  height: 1px;
}
.content_item{
  height: auto;
  display: grid;
  grid-template-columns: 1fr 5fr;
}
.content_item_left{
  height: auto;
  position: relative;
}
.avatar_img{
  position: absolute;
  top: 15px;
  left: calc(50% - 20px);
  height: 50px;
  width: 50px;
}
.content_item_right{
  height: auto;
  border-bottom: 0.1px solid #dadada;
  padding-bottom: 10px;
  width: 90%;
  padding-top: 10px;
  display: grid;
  grid-template-rows: 2fr auto 30px;
}
.item_info{
  display: grid;
  grid-template-rows: auto 1fr;
}
.p_top{
  display: flex;
  .p_username{
    margin: 0 10px 0 0;
    font-weight: bold;
  }
  .p_postTime{
    color: #999999;
  }
}
.p_bottom{
  margin-top: 2px;
}
.p_img{
  margin-top: 10px;
  img{
    max-width: 100%;
    border-radius: 10px;
  }
}
.item_control{
  display: flex;
  height: 20px;
  margin: 10px 0 10px 0;
  .item_control_item{
    :hover{
      animation: pulse 0.5s;
    }
    margin-right: 16px;
  }
  .item_control_item i{
    font-size: 18px;
    margin-right: 4px;
    color: #54626F;
  }
  .item_control_item p{
    font-size: 14px;
    margin-top: 1px;
    color: #54626F;
  }
}
</style>